/*
		ServEngine framework library
		A library to fast develop JEE online applications
	
		Copyright 2008 José Ignacio de Córdoba Álvaro

		Licensed under the Apache License, Version 2.0 (the "License");
		you may not use this file except in compliance with the License.
		You may obtain a copy of the License at

				http://www.apache.org/licenses/LICENSE-2.0

		Unless required by applicable law or agreed to in writing, software
		distributed under the License is distributed on an "AS IS" BASIS,
		WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
		See the License for the specific language governing permissions and
		limitations under the License.
	
		Jose Ignacio de Cordoba Alvaro
		http://ignacio.decordoba.com
*/

package com.servengine.user;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Transient;

@Entity
@NamedQueries ({
	@NamedQuery(name = "UserSession.findByUserid", query = "select o from UserSession o where o.user.portal.id = :portalid and o.user.userid = :userid order by o.start desc"),
	@NamedQuery(name = "UserSession.findByPortalid", query = "select o from UserSession o where o.user.portal.id = :portalid order by o.start desc"),
	@NamedQuery(name = "UserSession.findBySearchstring", query = "select o from UserSession o where o.user.portal.id = :portalid and lcase(o.user.userid) like lcase(:searchstring) order by o.start desc"),
	@NamedQuery(name = "UserSession.findAll", query = "select o from UserSession o order by o.start desc")
})
public class UserSession implements java.io.Serializable,Comparable<UserSession>
{
	protected Integer id;
	protected java.util.Date start,end;

	protected User user;
	public UserSession() {}
	public UserSession(User user)
	{
		start = new java.util.Date();
		this.user=user;
	}

	public int compareTo(UserSession o)
	{
		return - start.compareTo(o.getStart());
	}

	@Id @GeneratedValue(strategy=GenerationType.AUTO)
	public void setId(Integer id)
	{
		this.id = id;
	}

	@Id @GeneratedValue
	public Integer getId()
	{
		return id;
	}

	public void setStart(Date start)
	{
		this.start = start;
	}

	public Date getStart()
	{
		return start;
	}

	public void setEnd(Date end)
	{
		this.end = end;
	}

	public Date getEnd()
	{
		return end;
	}

	@Transient
	public String getPortalid()
	{
		return user.getPortal().getId();
	}

	@Transient
	public String getUserid()
	{
		return user.getUserid();
	}

	@ManyToOne(fetch=FetchType.EAGER, optional=false)
	public User getUser()
	{
		return user;
	}
	public void setUser(User user)
	{
		this.user = user;
	}
	private static final String encodingPassword = "123";
	@Transient
	public String getEncodedId()
	{
		String[] values = new String[4];
		values[0] = getPortalid();
		values[1] = getUserid();
		values[2] = ""+id;
	  values[3] = getPortalid() + getUserid() + encodingPassword;
		return UserManagerBean.getMessageDigest(values);
	}
	public void checkEncodedId(String encodedId)
	{
		if (!getEncodedId().equals(encodedId))
			throw new IllegalArgumentException();
	}
}
